#include "../atomic_include/MotorData.h"


/**
 * åä¾æ¨¡å¼çæä¸ä¸ªMotorç±»ï¼åå«åºæ¬çå½æ°ååé
 */
// å®ç°è·ååä¾å®ä¾çéææ¹æ³
// å®ç°è·ååä¾å®ä¾çéææ¹æ³
MotorList& MotorList::getInstance() {
    static MotorList instance;
    return instance;
}

// æ·»å æ°ççµæº
void MotorList::addMotor(int id) {
    // å¦æå­å¨ç¸å ID ççµæºï¼åä¸æ·»å 
    for (const auto& motor : motors) {
        if (motor.motor_id == id) {
            return;
        }
    }
    motors.emplace_back(id);
}

// è®¾ç½®æå®çµæºçéåº¦
void MotorList::setSpeed(int motor_id, double speed) {
    for (auto& motor : motors) {
        if (motor.motor_id == motor_id) {
            motor.speed_ = speed;
            return;
        }
    }
}

// è·åæå®çµæºçéåº¦
double MotorList::getSpeed(int motor_id) {
    for (const auto& motor : motors) {
        if (motor.motor_id == motor_id) {
            return motor.speed_;
        }
    }
    return -9999.0; // æªæ¾å°çµæºï¼è¿åé»è®¤å¼
}

// è®¾ç½®æå®çµæºçæ¸©åº¦
void MotorList::setTemperature(int motor_id, double temperature) {
    for (auto& motor : motors) {
        if (motor.motor_id == motor_id) {
            motor.temperature = temperature;
            return;
        }
    }
}

// è·åæå®çµæºçæ¸©åº¦
double MotorList::getTemperature(int motor_id) {
    for (const auto& motor : motors) {
        if (motor.motor_id == motor_id) {
            return motor.temperature;
        }
    }
    return -9999.0;  // æªæ¾å°çµæºï¼è¿åé»è®¤å¼
}

// è®¾ç½®æå®çµæºçç¶æ
void MotorList::setStatus(int motor_id, const std::string& status) {
    for (auto& motor : motors) {
        if (motor.motor_id == motor_id) {
            motor.status = status;
            return;
        }
    }
}

// è·åæå®çµæºçç¶æ
std::string MotorList::getStatus(int motor_id) {
    for (const auto& motor : motors) {
        if (motor.motor_id == motor_id) {
            return motor.status;
        }
    }
    return ""; // æªæ¾å°çµæºï¼è¿åç©ºå­ç¬¦ä¸²
}

// è·åæå®ç´¢å¼ççµæº ID
int MotorList::getMotorID(int index) {
    if (index >= 0 && index < static_cast<int>(motors.size())) {
        return motors[index].motor_id;
    }
    return -1; // ç´¢å¼è¶çï¼è¿å -1
}


int StartMotor(RequestSignal *requestSignal, OperationResult *operationResult){
    // å¯å¨ motor signal
    MotorList& motorList = MotorList::getInstance();
    int motor_id = requestSignal -> motor_id;
    motorList.addMotor(motor_id); // è¥çµæºä¸å­å¨åæ·»å 
    motorList.setSpeed(motor_id, 10.0);
    motorList.setTemperature(motor_id, 25.0);
    motorList.setStatus(motor_id, "started");
    operationResult->status_code = 1;
    operationResult->description = "Motor is started";
    return ATOMIC_SERVICE_SUCCESS;
}

int StopMotor(RequestSignal *requestSignal, OperationResult *operationResult){
    // åæ­¢ motor signal
    MotorList& motorList = MotorList::getInstance();
    int motor_id = requestSignal -> motor_id;
    motorList.setSpeed(motor_id, 0.0);
    motorList.setTemperature(motor_id, 0.0);
    motorList.setStatus(motor_id, "stopped");
    operationResult->status_code = 1;
    operationResult->description = "Motor is stopped";
    return ATOMIC_SERVICE_SUCCESS;
}

int statusDetect(RequestSignal *requestSignal, MotorResult *motorResult){
    // è·å motor status
    MotorList& motorList = MotorList::getInstance();
    int motor_id = requestSignal -> motor_id;
    motorResult->motor_id = motor_id;
    motorResult->temperature = motorList.getTemperature(motor_id);
    motorResult->speed = motorList.getSpeed(motor_id);
    motorResult->status = motorList.getStatus(motor_id);
    return ATOMIC_SERVICE_SUCCESS;
}

int SetMotorSpeed(RequestSpeed *requestSpeed, OperationResult *operationResult){
    // è®¾ç½® motor speed
    MotorList& motorList = MotorList::getInstance();
    int motor_id = requestSpeed->motor_id;
    motorList.setSpeed(motor_id, requestSpeed->speed);
    operationResult->status_code = 1;
    operationResult->description = "Motor speed is set to " + std::to_string(requestSpeed->speed);
    return ATOMIC_SERVICE_SUCCESS;
}


 void StartMotor_func(RequestSignal *request, OperationResult *reply){
 	// Function implementation TODO
     int ret_type = StartMotor(request, reply);
     reply->return_type = ret_type;
 }

 void StopMotor_func(RequestSignal *request, OperationResult *reply){
 	// Function implementation TODO
     int ret_type = StopMotor(request, reply);
     reply->return_type = ret_type;
 }

 void statusDetect_func(RequestSignal *request, MotorResult *reply){
 	// Function implementation TODO
     int ret_type = statusDetect(request, reply);
     reply->return_type = ret_type;
 }

 void SetMotorSpeed_func(RequestSpeed *request, OperationResult *reply){
 	// Function implementation TODO
     int ret_type = SetMotorSpeed(request, reply);
     reply->return_type = ret_type;
 }

